package com.example.samplemanagement.repository;

import com.example.samplemanagement.model.Customer;
import com.example.samplemanagement.model.Sample;
import com.example.samplemanagement.model.SampleTracking;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface SampleTrackingRepository extends JpaRepository<SampleTracking, Long> {

    // 查询特定样品和客户的所有跟踪记录，按日期降序
    List<SampleTracking> findBySampleAndCustomerOrderByTrackingDateDesc(Sample sample, Customer customer);

    // 查询特定样品ID和客户ID的所有跟踪记录，按日期降序 (方便直接通过ID查询)
    List<SampleTracking> findBySampleIdAndCustomerIdOrderByTrackingDateDesc(Long sampleId, Long customerId);

    // 查询某个样品-客户组合的最新一条跟踪记录
    Optional<SampleTracking> findFirstBySampleAndCustomerOrderByTrackingDateDesc(Sample sample, Customer customer);

    // 您不再需要 findLatestTrackingRecordsForEachSampleCustomerPair() 和 findDistinctSampleAndCustomerPairs()
    // 这些聚合和过滤逻辑现在主要在 SampleTrackingService 中通过内存处理
    // 在SampleTrackingRepository接口中添加
    List<SampleTracking> findBySampleIdAndCustomerId(Long sampleId, Long customerId);
}
